<template>
  <el-form
    ref="ruleFormRef"
    :model="phone"
    :rules="rules"
    class="demo-ruleForm"
    status-icon
  >
    <el-form-item label="手机号" prop="num">
      <el-input v-model="phone.num" />
    </el-form-item>
    <el-form-item label="验证码" prop="code">
      <div class="get-code">
        <el-input v-model="phone.code" width="60px" />
        <el-button
          type="primary"
          class="get-but"
          @click="submitForm(ruleFormRef)"
        >
          Create
        </el-button>
      </div>
    </el-form-item>
  </el-form>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { reactive } from 'vue'
import { useStore } from 'vuex'
import type { FormInstance } from 'element-plus'
import { rules } from '../config/account-config'

export default defineComponent({
  setup() {
    const state = useStore()
    const phone = reactive({
      num: '',
      code: ''
    })
    const ruleFormRef = ref<FormInstance>()
    const submitForm = async (formEl: FormInstance | undefined) => {
      if (!formEl) return
      await formEl.validate((valid, fields) => {
        if (valid) {
          console.log('submit!')
        } else {
          console.log('error submit!', fields)
        }
      })
    }
    const phoneAcction = async () => {
      ruleFormRef.value?.validate(valid => {
        console.log(valid)
        if (valid) {
          state.dispatch('login/phoneLoginAction')
          console.log('phoneAcction')
        }
      })
    }

    return { rules, phone, submitForm, ruleFormRef, phoneAcction }
  }
})
</script>

<style scoped>
.get-code {
  display: flex;
}
.get-but {
  margin-left: 15px;
}
</style>
